package com.bank.gui;

import com.bank.utils.CommunicationWrapper;
import com.bank.utils.Operation;
import com.bank.utils.TextFieldLimiter;
import com.bank.utils.Toast;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.AbstractDocument;
import org.json.JSONException;
import org.json.JSONObject;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Moofie
 */
public class TransferPanel extends javax.swing.JPanel {

    private CommunicationWrapper cw;
    private String branchCode;
    private int portNo;

    /**
     * Creates new form TransferPanel
     */
    public TransferPanel(CommunicationWrapper cw, String branchCode, int portNo) {
        this.cw = cw;
        this.branchCode = branchCode;
        this.portNo = portNo;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtAccNo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtAccNoReceiver = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtAmountTransfer = new javax.swing.JTextField();
        btnReset = new javax.swing.JButton();
        btnSubmit = new javax.swing.JButton();
        jlblAccBalance = new javax.swing.JLabel();
        jlblBalance = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Account Number");
        add(jLabel1);
        jLabel1.setBounds(20, 40, 190, 30);

        AbstractDocument aDocAccNo = (AbstractDocument)txtAccNo.getDocument();
        aDocAccNo.setDocumentFilter(new TextFieldLimiter("\\d{0,14}"));
        txtAccNo.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        add(txtAccNo);
        txtAccNo.setBounds(20, 70, 400, 30);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Beneficial Account Number ");
        add(jLabel2);
        jLabel2.setBounds(20, 110, 220, 30);

        AbstractDocument aDocAccReceive = (AbstractDocument)txtAccNoReceiver.getDocument();
        aDocAccReceive.setDocumentFilter(new TextFieldLimiter("\\d{0,14}"));
        txtAccNoReceiver.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        add(txtAccNoReceiver);
        txtAccNoReceiver.setBounds(20, 140, 400, 30);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Amount to Transfer");
        add(jLabel3);
        jLabel3.setBounds(20, 180, 180, 30);

        AbstractDocument aDocAmt = (AbstractDocument)txtAmountTransfer.getDocument();
        aDocAmt.setDocumentFilter(new TextFieldLimiter("\\d{0,}"));
        txtAmountTransfer.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        add(txtAmountTransfer);
        txtAmountTransfer.setBounds(20, 210, 400, 30);

        btnReset.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });
        add(btnReset);
        btnReset.setBounds(20, 320, 160, 50);

        btnSubmit.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnSubmit.setText("Submit");
        btnSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubmitActionPerformed(evt);
            }
        });
        add(btnSubmit);
        btnSubmit.setBounds(260, 320, 160, 50);

        jlblAccBalance.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblAccBalance.setText("Account Balance                        : ");
        add(jlblAccBalance);
        jlblAccBalance.setBounds(20, 250, 210, 30);

        jlblBalance.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        add(jlblBalance);
        jlblBalance.setBounds(230, 250, 180, 30);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("TRANSFER MONEY");
        add(jLabel5);
        jLabel5.setBounds(20, 5, 400, 20);
        add(jSeparator1);
        jSeparator1.setBounds(10, 25, 420, 2);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed
        //get all data from text field
        Double amount = Double.parseDouble(txtAmountTransfer.getText().trim());
        String accNo = txtAccNo.getText().trim();
        String accReceiver = txtAccNoReceiver.getText().trim();

        try {
            // constructing data
            JSONObject j = new JSONObject();
            // specific operation
            j.put("operation", Operation.TRANSFER);
            JSONObject content = new JSONObject();
            // put in necessary data
            content.put("accNo", accNo);
            content.put("accReceiver", accReceiver);
            content.put("amount", amount);
            content.put("port", this.portNo);
            content.put("bCode", this.branchCode);
            content.put("address", InetAddress.getLocalHost().getHostAddress());
            j.put("content", content);
            // after finished constructing data, send to server
            cw.send(j, InetAddress.getLocalHost(), 5000);
            // wait for reply from server
            JSONObject js = cw.receive();
            JSONObject result = js.getJSONObject("content");
            if (result.get("result").toString().equalsIgnoreCase("Success")) {
                // operation succeed, display successful message and returned value
                Toast.makeText(getParent(), "Success", Toast.LENGTH_SHORT).display();
                jlblBalance.setText("RM " + String.format("%.2f", result.getDouble("balance")));
            } else {
                // operation failed, display error message
                Toast.makeText(getParent(), "Transfer Unsuccessful.", Toast.LENGTH_SHORT).display();
            }
        } catch (JSONException ex) {
            ex.printStackTrace();
            System.out.println(ex);
        } catch (UnknownHostException ex) {
            Logger.getLogger(TransferPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_btnSubmitActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        // clear all field
        txtAccNo.setText("");
        txtAccNoReceiver.setText("");
        txtAmountTransfer.setText("");
    }//GEN-LAST:event_btnResetActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSubmit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel jlblAccBalance;
    private javax.swing.JLabel jlblBalance;
    private javax.swing.JTextField txtAccNo;
    private javax.swing.JTextField txtAccNoReceiver;
    private javax.swing.JTextField txtAmountTransfer;
    // End of variables declaration//GEN-END:variables
}
